# include "stdlib.h"
# include "string"
# include "data_struct.h"
# include "set"
# include "vector"
# include "math.h"
# include "IO.h"
# include "string.h"
# include "stdio.h"
using namespace std;

int main(int argn,char **argv)
{
	
	FILE * test_file = NULL;
	char   model_file_name[1024];

	//usage ./test -t data -m model
	if(5 != argn)
	{
		fprintf(stderr,"[ERROR] usage:./test -d data_file -m model_file\n");
		return -1;
	}
	
	for(int i = 1;i < 5 ;i += 2)
	{
		if(0 == strcmp(argv[i],"-d"))
		{
			test_file = fopen(argv[i+1],"r");
			if(NULL == test_file)
			{
				fprintf(stderr,"[ERROR]:unable to open data file[%s]\n",argv[i+1]);
				return -1;
			}
		}
		else if(0 == strcmp(argv[i],"-m"))
		{
			strcpy(model_file_name,argv[i+1]);
		}
		else
		{
			fprintf(stderr,"[ERROR] usage:./test -d data_file -m model_file\n");
			return -1;
		}
	}
	
        //read model
	map<string,int> * feature_name_id = new map<string,int>();
	vector<double> * weight = new vector<double>();
	double beta0 = 0.0;
	read_model(model_file_name,feature_name_id,weight,&beta0);

	//read data and test
	Instance instance;
	while(read_data_line(test_file,feature_name_id,&instance))
	{
		double sum = beta0;
		for(set<int>::iterator iter = instance.feature.begin(); iter != instance.feature.end(); iter++)
		{
			sum += (*weight)[*iter];
		}
		double p = 1/(1+exp(-1*sum));
		printf("%lf\n",p);

		instance.feature.clear();
	}
	fclose(test_file);
	
	delete(feature_name_id);
	delete(weight);

}
